package org.example.JUC.unsafe;


import javafx.scene.control.Cell;
import sun.misc.Unsafe;

import java.lang.reflect.Field;
import java.util.concurrent.atomic.LongAdder;

/**
 * @description:
 * @author: farprt
 * @create: 2022-08-19 13:46
 **/
public class unsafeTest {

    static Unsafe unsafe;
    static long stateOffset;
    private volatile long state=0;
    static {
        try{
            //返回本类中声明的指定名称的字段，找不出抛出异常
            Field field=Unsafe.class.getDeclaredField("theUnsafe");
            field.setAccessible(true);
            //获得指定对象obj中该字段的值
            unsafe= (Unsafe) field.get(null);
            stateOffset=unsafe.objectFieldOffset(unsafeTest.class.getDeclaredField("state"));
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
            throw new Error(e);
        }
    }

    public static void main(String[] args) {
        unsafeTest test=new unsafeTest();
        Boolean success=unsafe.compareAndSwapInt(test,stateOffset,0,1);
        System.out.println(success);

        LongAdder longAdder=new LongAdder();
    }

}